package com.ftg.midamall.admin.mapper;

import com.ftg.midamall.admin.dto.AdminInfo;
import com.ftg.midamall.admin.dto.ModifyAdminInfo;
import com.ftg.midamall.admin.entity.UmsAdmin;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ftg.midamall.admin.entity.UmsAdminRoleRelation;
import com.ftg.midamall.admin.vo.UmsAdminVo;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 后台用户表 Mapper 接口
 * </p>
 *
 * @author qjw
 * @since 2023-10-20
 */
@Mapper
public interface UmsAdminMapper extends BaseMapper<UmsAdmin> {

    /**
     * 当用户登陆的时候用来更新登陆时间
     * @param id   用户id
     * @return  true
     */
    @Update("update ums_admin set login_time = now() where id = #{id}")
    boolean updateLoginTime(@Param("id") Long id);

    /**
     * 添加用户
     * @param
     * @return  true
     */
    @Insert("insert into ums_admin(username,password,icon,email,nick_name,note,status) values (#{u.username},#{u.password},#{u.icon},#{u.email},#{u.nick_name},#{u.note},#{u.status})")
    Integer insertAdmin(@Param("u") AdminInfo u);

    /**
     * 按用户名查找
     * @param
     * @return  true
     */
    @Select("select id,username,password,icon,email,nick_name,note,create_time,login_time,status from ums_admin where username=#{username}")
    UmsAdmin selectUserName(@Param("username") String username);

    /**
     * 更改密码
     * @param id   用户id
     * @return  true
     */
    @Update("update ums_admin set password=#{password} where id=#{id}")
    Integer modifyAdminPass(@Param("id") int id,@Param("password") String password);

    /**
     * 修改用户信息
     * @param
     * @return  true
     */

    @Update("update ums_admin set username=#{ur.username},email=#{ur.email},nick_name=#{ur.nick_name},note=#{ur.note},status=#{ur.status} where id=#{id}")
    int updateById(@Param("ur") AdminInfo ur , @Param("id") Long id);

    /**
     * 添加role_id
     * @param u
     * @return
     */
    @Insert("insert into ums_admin_role_relation(id,admin_id,role_id) values (#{u.id},#{u.admin_id},#{u.role_id})")
    Integer insertRole(@Param("u") UmsAdminRoleRelation u);


    void deleteAllAdmin(List<Integer> id);

    void deleteAllAdminRole(List<Integer> ids);

    /**
     * 删除用户时删除ums_admin_role_relation
     * @param admin_id
     * @return
     */
    @Delete("delete from ums_admin_role_relation where admin_id=#{admin_id}")
    int delAdmin(@Param("admin_id") long admin_id);

    /**
     * 切换状态
     * @param id
     * @return
     */
    @Update("update ums_admin set status=if(status=0,1,0) where id=#{id}")
    Boolean switchStatus(@Param("id") int id);
//
//    @Insert("insert into ums_admin icon values #{icon}")
//    Integer uploadIcon(@Param("icon") MultipartFile icon);

    /**
     * 修改头像
     * @param id   用户id
     * @param url  头像url
     * @return  true
     */
    @Update("update ums_admin set icon = #{url} where id = #{id}")
    boolean modifyAvatar(@Param("id")Long id, @Param("url") String url);

    /**
     * 修改用户信息
     * @param user  用户信息
     * @return  true
     */
    Boolean modifyUmsAdminWithNonNull(UmsAdmin user);

    /**
     * 删除用户时删除ums_admin_role_relation
     * @param admin_id
     * @return
     */

}
